<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('项目名称')" prop="dealId">
            <MainBodySelect
              v-model="form.dealId"
              parameters="deal"
              @change="dealIdChange"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('公司名称')" prop="companyId">
            <el-input v-model="form.companyName" disabled />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('委托评估/审计单位')" prop="auditUnit">
            <el-input v-model="form.auditUnit" :placeholder="$t('委托评估/审计单位')" show-word-limit maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('中介报价') + $t('金额单位')" prop="offer">
            <Money v-model="form.offer" :placeholder="$t('中介报价')" unit="w" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('中介选聘日期') " prop="recruitingTime">
            <el-date-picker
              v-model="form.recruitingTime"
              type="date"
              placeholder="选择日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('服务团队')" prop="deptId">
            <el-select v-model="form.deptId" :placeholder="$t('服务团队')" clearable>
              <el-option
                v-for="dict in deptOptions"
                :key="dict.id"
                :label="$t(dict.deptName)"
                :value="dict.id"
              />
            </el-select>
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button v-hasPermi="['mediation:employ:edit']" type="primary" @click="submitForm">{{ $t("保存") }}</el-button>
    </div>
  </div>
</template>

<script>
import { saveMediationEmployRecord, getMediationEmployRecord } from '@/api/deal/mediation/employ/mediationEmployRecord'
import { findDeptList } from '@/api/deal/mediation/mediationTeam/mediationTeam'
import MainBodySelect from '@/components/MainBodySelect/index'
import { getDealBase } from '@/api/base/deal/dealBase'
export default {
  components: {
    MainBodySelect
  },
  data() {
    return {
      // 表单参数
      form: {},
      // 遮罩层
      loading: false,
      // 服务团队选项
      deptOptions: [],
      // 表单校验
      rules: {
        dealId: { required: true, message: this.$t('必填信息'), trigger: 'blur' },
        offer: { required: true, message: this.$t('必填信息'), trigger: 'blur' }
      }
    }
  },
  created() {
    const child = {
      mediationId: this.$route.query.id
    }
    findDeptList(child).then(response => {
      this.deptOptions = response.data
    })
  },
  methods: {
      // 初始化数据
      init(data) {
          if (data != null && data.id !== undefined) {
              getMediationEmployRecord(data.id).then(response => {
                  this.form = response.data
              })
          } else {
              this.reset()
          }
      },
      // 表单重置
      reset() {
          this.form = {
          id: undefined,
          dealId: undefined,
          companyId: undefined,
          auditUnit: undefined,
          offer: undefined,
          createBy: undefined,
          updateBy: undefined,
          createTime: undefined,
          updateTime: undefined,
          rate: 0,
          mediationId: this.$route.query.id,
            deptId: undefined
          }
          this.resetForm('form')
      },
      // 取消按钮
      cancel() {
          this.$parent.$parent.formOpen = false
          this.$parent.$parent.reset()
      },
      // 关闭form页面
      closeForm() {
          // 关闭form页面遮罩层
          this.loading = false
          // 关闭form页面
          this.$parent.$parent.formOpen = false
          // 刷新list页面
          this.$parent.$parent.getList()
      },
      /** 提交按钮 */
      submitForm: function() {
          this.$refs['form'].validate(valid => {
              if (valid) {
                  this.loading = true
                  saveMediationEmployRecord(this.form).then(response => {
                      if (response.code === 200) {
                          this.msgSuccess(this.$t('保存成功'))
                          this.closeForm()
                      } else {
                          this.msgError(response.msg)
                      }
                      this.loading = false
                  })
              } else {
                this.locationError(document)
              }
          })
      },
    dealIdChange(val) {
      getDealBase(val).then(res => {
        this.$set(this.form, 'companyId', res.data.company.id)
        this.$set(this.form, 'companyName', res.data.company.companyName)
      })
    }
  }
}
</script>
